package b4.model.player
{
	import b4.model.basic.*;
	import b4.model.config.*;
	import b4.model.obj.*;
	import b4.net.*;
	
	import flash.events.Event;
	import flash.external.*;
	import flash.net.*;
	import flash.utils.*;

	public class PackageModel extends Model
	{
		public var ownerId:int;
		
		public var objectList:Object;
		
		public function PackageModel()
		{
			super();
			objectList = new Object();
			ownerId = -1;
		}
		
		public function getObjectNum(objectIndex:int):int 
		{
			return objectList[objectIndex] == null ? 0 : objectList[objectIndex].objectNum;
		}
		
		public function getPackage(force:Boolean = false):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.GET_PACKAGE, {ownerId:ownerId});
			serverRequest.addEventListener(ServerEvent.GET_PACKAGE, onGetPackageComplete);
			serverRequest.post(force);
		}
		
		public function onGetPackageComplete(event:ServerEvent):void
		{
			var jsonObj:Object = event.jsonObj;
			var objects:Object = jsonObj.data.packageInfo;
			objectList = new Object();
			for (var s:String in objects)
			{
				var obj:ObjModel = new ObjModel();
				obj.loadbyObject(objects[s]);
				objectList[s] = obj;
			}
			var modelEvent:ModelEvent = new ModelEvent(ModelEvent.PACKAGE_REFRESH);
			dispatchEvent(modelEvent);
		}
		
	}
}